<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Auth;
class UserController extends Controller
{
    // 显示
    public function index(User $user) {
    	$u = User::find(Auth::id());
    	$users = $user -> where('active',1) -> paginate(10);
    	return view('admin.user',compact('users','u'));
    }

    // 个人档案
    public function profile($id) {
    	$u = User::find(Auth::id());
    	$user = User::find($id);
    	return view('admin.profile',compact('user','u'));
    }

    // 编辑
    public function edit(Request $request,$id) {
    	$user = User::find($id);
      
        $data = $request->only('uname','phone','email');
        $path = 'uploads/uimgs/';
        if($request -> hasFile('pic') && $request -> pic -> isValid()) {
            $filename = md5(time()).'.'.$request->pic->extension();
            if($request -> pic -> move($path, $filename)) {
                $path .= $filename;
                $data['uimg'] = $path;
            }
        }
        if($user -> update($data)) {
            return redirect('/admin/user/profile/'.$id)->with('status','修改成功');
        }
    }

    // 删除
    public function del($id) {
    	$user = User::find($id);
    	if($user -> update(['active'=>0])) {
    		return true;
    	}else {
    		return false;
    	}
    }
}
